package sk.adresa.eaukcia.core.dao.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;

import sk.adresa.eaukcia.core.dao.AuctionDao;
import sk.adresa.eaukcia.core.data.Auction;
import sk.adresa.eaukcia.core.query.Paging;
import sk.adresa.eaukcia.core.data.filter.AuctionFilter;
import sk.adresa.eaukcia.core.query.PaginatedList;

public class AuctionDaoImpl extends AbstractDao implements AuctionDao {

    private static final String DEFAULT_PREFIX = "sk.adresa.eaukcia.core.dao.impl.AuctionMapper.";

    public AuctionDaoImpl() {
    }

    @Override
    public Auction getAuction(int auctionId) {
        Object object = sqlSession.selectOne(DEFAULT_PREFIX + "getAuction", auctionId);
        return object instanceof Auction ? ((Auction) object) : null;
    }

    @Override
    public PaginatedList<Auction> getFilteredAuctions(AuctionFilter filter, Paging paging) {

        int totalRows = (Integer) sqlSession.selectOne(DEFAULT_PREFIX + "countFilteredAuctions", filter);
        Paging pp = new Paging(paging.getRowsPerPage(), paging.getActualPage(), totalRows);
        RowBounds bounds = new RowBounds(pp.getOffset(), pp.getRowsPerPage());
        List auctions = sqlSession.selectList(DEFAULT_PREFIX + "getFilteredAuctions", filter, bounds);
        return new PaginatedList<Auction>(auctions, pp);

    }

}
